package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConst;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.CheckItem;
import com.itheima.pojo.Permission;
import com.itheima.service.PermissionService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author Administrator
 * @Date: 2020/3/7 14:40
 * @Description:
 */
@RestController
@RequestMapping("/permission")
@Slf4j
public class PermissionController {

    @Reference
    PermissionService permissionService;

    @RequestMapping("/add")
    public Result add (@RequestBody Permission permission){
        log.debug("PermissionController:add:"+permission);
        permissionService.add(permission);
        log.debug("权限添加成功!!!");
        //只返回正确的结果,错误不用管,因为有自定义异常
        return new Result(true,"权限添加成功!!!");
    }

    @RequestMapping("/findPage")
    public Result findPage(@RequestBody QueryPageBean queryPageBean){
        log.debug("queryPageBean:"+queryPageBean);
        //需要返回给前端的是PageResult,里面有total和rows
        PageResult pageResult= permissionService.findPage(queryPageBean);
        log.debug("权限分页查询成功");
        return new Result(true,"权限分页查询成功",pageResult);

    }

    @RequestMapping("/delById")
    public Result delById(Integer id){
        log.debug("delById:"+id);
        //不需要结果接收
        permissionService.delById(id);
        log.debug("权限根据id删除成功");
        return new Result(true,"权限根据id删除成功");
    }

    @RequestMapping("findById")
    public Result findById(Integer id){
        log.debug("findById:"+id);
        Permission permission= permissionService.findById(id);
        log.debug("根据id查询权限成功");
        return new Result(true,"根据id查询权限成功",permission);
    }

    @RequestMapping("/edit")
    public Result edit(@RequestBody Permission permission){
        log.debug("edit: " + permission);
        permissionService.edit(permission);
        log.debug("权限修改成功!!");
        return new Result(true, "权限修改成功!!");
    }
}
